import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: EditDynamicPage(),
    );
  }
}

class EditDynamicPage extends StatefulWidget {
  const EditDynamicPage({super.key});

  @override
  _EditDynamicPageState createState() => _EditDynamicPageState();
}

class _EditDynamicPageState extends State<EditDynamicPage> {
  final TextEditingController _textController = TextEditingController();
  final List<XFile> _mediaFiles = [];
  late String _selectedTopic;
  final ImagePicker _picker = ImagePicker();

  void _pickMedia() async {
    final XFile? image = await _picker.pickImage(source: ImageSource.gallery);
    if (image != null) {
      setState(() {
        _mediaFiles.add(image);
      });
    }
  }

  void _pickVideo() async {
    final XFile? video = await _picker.pickVideo(source: ImageSource.gallery);
    if (video != null) {
      setState(() {
        _mediaFiles.add(video);
      });
    }
  }

  void _selectTopic() {
    // Implement topic selection logic
  }

  void _selectEmoji() {
    // Implement emoji selection logic
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('发布动态'),
        actions: [
          IconButton(
            icon: const Icon(Icons.send),
            onPressed: () {
              // Implement send logic
            },
          ),
        ],
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            TextField(
              controller: _textController,
              maxLines: 6,
              decoration: const InputDecoration(
                hintText: '说点什么吧...',
                border: OutlineInputBorder(),
              ),
            ),
            const SizedBox(height: 10),
            Row(
              children: [
                IconButton(
                  icon: const Icon(Icons.photo),
                  onPressed: _pickMedia,
                ),
                IconButton(
                  icon: const Icon(Icons.video_call),
                  onPressed: _pickVideo,
                ),
                IconButton(
                  icon: const Icon(Icons.tag),
                  onPressed: _selectTopic,
                ),
                IconButton(
                  icon: const Icon(Icons.emoji_emotions),
                  onPressed: _selectEmoji,
                ),
              ],
            ),
            const SizedBox(height: 10),
            Wrap(
              children: _mediaFiles.map((file) {
                return Padding(
                  padding: const EdgeInsets.all(4.0),
                  child: Image.file(
                    File(file.path),
                    width: 100,
                    height: 100,
                    fit: BoxFit.cover,
                  ),
                );
              }).toList(),
            ),
          ],
        ),
      ),
    );
  }
}
